import { useEffect, useState } from "react";

import { get } from "lodash";

import { LANGUAGES } from "shared/constants";

import { en } from "../config/dictionaries/en";
import { ru } from "../config/dictionaries/ru";

export const useTranslete = () => {
	const [dictionary, setDictionar] = useState<typeof ru | typeof en>(ru);
	const [choice, setChoice] = useState(LANGUAGES.RU);

	const t = (path: string) => {
		return get(dictionary, path, path);
	};

	const handleChangeLanguage = (value: LANGUAGES) => {
		setChoice(value);
	};

	useEffect(() => {
		if (choice === LANGUAGES.EN) {
			setDictionar(en);
		}
		if (choice === LANGUAGES.RU) {
			setDictionar(ru);
		}
	}, [choice]);

	return { t, handleChangeLanguage, language: choice };
};
